package com.broad.commerce.code.base;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.broad.commerce.code.modules.BaseModule;
import com.broad.commerce.code.modules.jdbc.MapperModule;
import com.broad.commerce.common.utils.core.StringUtils;
import com.broad.commerce.web.exception.ServiceException;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * The type Abstract base service.
 * @author stephen
 * @version V1.0
 * @date 2022 -02-07 13:00
 * @description: AbstractBaseService
 */
@SuppressWarnings("ResultOfMethodCallIgnored")
public abstract class AbstractBaseService {
	/**
	 * Create class code string.
	 * @param baseModule the base module
	 * @return the string
	 */
	public abstract String createClassCode(BaseModule baseModule);

	/**
	 * Create xml code string.
	 * @param baseModule the base module
	 * @return the string
	 */
	public abstract String createXmlCode(BaseModule baseModule);

	/**
	 * Create class file.
	 * @param baseModule the base module
	 * @param rootPath the root path
	 * @param fileType the file type
	 */
	public void createClassFile(BaseModule baseModule, String rootPath, String fileType) {
		if (StringUtils.isEmpty(rootPath)) {
			throw new ServiceException("文件路径不能为空！");
		}
		File file = null;
		String content;
		try {
			String fileName = baseModule.getClassName();
			if (baseModule instanceof MapperModule) {
				content = this.createXmlCode(baseModule);
				if (rootPath.charAt(rootPath.length() - 1) == '/') {
					file = new File(rootPath + fileName + "." + fileType);
				} else {
					file = new File(rootPath + "/" + fileName + "." + fileType);
				}
			} else {
				content = this.createClassCode(baseModule);
				String packagePath = baseModule.getPackageName().replace(".", "/") + "/";
				file = new File(rootPath + packagePath + fileName + "." + fileType);
				System.out.println("filePath => " + rootPath + packagePath + fileName + "." + fileType);
			}
			if (file.exists()) {
				if (!baseModule.getUpdate()) {
					return;
				}
				file.delete();//删除文件
			}
			File fileParent = file.getParentFile();//返回的是File类型,可以调用exsit()等方法
			String fileParentPath = file.getParent();//返回的是String类型
			if (!fileParent.exists()) {
				fileParent.mkdirs();// 能创建多级目录
			}
			file.createNewFile();//有路径才能创建文件
			FileUtil.appendString(content, file, StandardCharsets.UTF_8);
			System.out.println(file);
			System.out.println(content);
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		} finally {
			if (file != null) {
				IoUtil.closeIfPosible(file);
			}
		}
	}

	/**
	 * Create batch class files.
	 * @param baseModules the base modules
	 * @param rootPath the root path
	 */
	public void batchCreateClassFiles(List<BaseModule> baseModules, String rootPath, String fileType) {
		if (baseModules.isEmpty()) {
			return;
		}
		if (StringUtils.isEmpty(rootPath)) {
			throw new ServiceException("文件路径不能为空！");
		}
		baseModules.forEach(baseModule -> this.createClassFile(baseModule, rootPath, fileType));
	}
}
